package com.example.im.server.im

import android.text.TextUtils
import cc.vv.lklibrary.log.LogOperate
import com.example.baselibrary.db.dao.GroupDao
import com.example.baselibrary.db.dao.GroupMemberDao
import com.example.baselibrary.db.table.GroupMemberTable
import com.example.baselibrary.db.table.GroupTable
import com.example.im.bean.GroupInfoObj
import com.example.im.bean.GroupListObj
import com.example.im.bean.GroupMemberObj
import java.util.ArrayList

/**
 * Im数据组装
 * @author  chaohaipeng  on 2018/7/25.
 */
class IMOtherService {
    private object mHolder {
        val instance = IMOtherService()
    }
    companion object {
        fun getInstance(): IMOtherService {
            return mHolder.instance
        }
    }

    /**
     * 组装群组成员id
     *
     * @param groupId 群组id
     */
    fun assembleGroupMemberId(groupId: String): String {
        val memberIds = StringBuffer()
        if (!TextUtils.isEmpty(groupId)) {
            val groupMemberTables = GroupMemberDao.getInstance().queryByColumn("groupId", groupId)
            for (groupMemberTable in groupMemberTables) {
                memberIds.append(String.format("%s,", groupMemberTable.account))
            }
            LogOperate.eT("aaaaaa", memberIds.toString())
        }
        return if (memberIds.toString().endsWith(",")) memberIds.subSequence(0, memberIds.length - 1).toString() else memberIds.toString()
    }


    /**
     * 更新群组信息、成员信息
     *
     * @param groupInfoObj
     */
    fun updateGroup(groupInfoObj: GroupInfoObj) {
        /*本地保存群信息*/
        val table = GroupTable(groupInfoObj.id, groupInfoObj.name, groupInfoObj.faceUrl, groupInfoObj.affiliationsCount)
        GroupDao.getInstance().createOrUpdate(table)
        /*本地保存群组成员信息*/
        updateGroupMember(groupInfoObj.id, groupInfoObj.groupUserList)
    }

    /**
     * 本地保存群组成员信息
     *
     * @param groupId
     * @param groupMembers
     */
    private fun updateGroupMember(groupId: String, groupMembers: ArrayList<GroupMemberObj>?) {
        if (null != groupMembers && !groupMembers.isEmpty()) {
            val groupMemberTables = ArrayList<GroupMemberTable>()
            for (groupMember in groupMembers) {
                val groupMemberTable = GroupMemberTable()
                groupMemberTable.groupId = groupId
                if (groupMember != null) {
                    groupMemberTable.account = groupMember.id
                    groupMemberTable.nick = groupMember.nick
                    groupMemberTable.avatar = groupMember.faceUrl
                }
                groupMemberTables.add(groupMemberTable)
            }
            GroupMemberDao.getInstance().insert(groupId, groupMemberTables)
        }
    }

    /**
     * 群组列表接口更新本地群组信息
     *
     * @param groupListObjs
     */
    fun updateGroupList(groupListObjs: List<GroupListObj>?) {
        GroupDao.getInstance().deleteAll()
        if (null != groupListObjs && !groupListObjs.isEmpty()) {
            for (groupListObj in groupListObjs) {
                /*本地保存群信息*/
                val table = GroupTable(groupListObj.id, groupListObj.name, groupListObj.faceUrl, groupListObj.count)
                GroupDao.getInstance().createOrUpdate(table)
                updateGroupMember(groupListObj.id, groupListObj.groupUserList)
            }
        }
    }


}